<template>
  <div class="register">
    <cu-header :height="80" />
    <div class="register-container container">
      <div class="register-form">
        <p class="title">{{ $t('register.1') }}</p>
        <div class="form">
          <el-form ref="form" :model="form" :rules="rules" label-width="150px">
            <el-form-item :label="$t('register.2')" prop="avatar">
              <el-upload class="avatar-uploader" :action="uploadUrl" :show-file-list="false" :on-success="handleAvatarSuccess">
                <img v-if="form.avatar" :src="form.avatar.startsWith('http')?form.avatar:(domain + form.avatar)" class="avatar)" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item :label="$t('register.3')" prop="email">
              <el-input type="text" v-model="form.email" :placeholder="$t('register.12')"></el-input>
            </el-form-item>
            <el-form-item :label="$t('register.4')" prop="mobile">
              <el-input type="text" v-model="form.mobile" :placeholder="$t('register.13')"></el-input>
            </el-form-item>
            <el-form-item :label="$t('register.5')" prop="password">
              <el-input type="password" v-model="form.password" :placeholder="$t('register.14')"></el-input>
            </el-form-item>
            <el-form-item :label="$t('register.6')" prop="rePassword">
              <el-input type="password" v-model="form.rePassword" :placeholder="$t('register.15')"></el-input>
            </el-form-item>
            <el-form-item :label="$t('register.7')" prop="no">
              <el-input type="text" v-model="form.no" :placeholder="$t('register.16')"></el-input>
            </el-form-item>
            <el-form-item :label="$t('register.8')" prop="area">
              <el-cascader ref="area" v-model="form.area" :options="multiOptions" :props="{ value: 'code', label: 'name' }" :placeholder="$t('register.17')" @change="handleMultiChange" style="width: 100%"></el-cascader>
            </el-form-item>
<!--            <el-form-item :label="$t('register.9')" prop="address">-->
<!--              <el-input type="text" v-model="form.address" :placeholder="$t('register.18')"></el-input>-->
<!--            </el-form-item>-->
            <el-form-item class="text-center">
              <el-button type="default" @click="handleBack">{{ $t('register.10') }}</el-button>
              <el-button type="primary" @click="handleSubmit">{{ $t('register.11') }}</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div @click="show = true" class="go-login">Já tem uma conta para entrar</div>
      </div>
    </div>
    <cu-footer />
    <!-- 登陆弹框 -->
    <el-dialog top="5vh" :title="title" :visible.sync="show" :width="width">
      <div class="modal-form">
        <div class="login-title">{{ translate('component.login.1') }}</div>
        <el-form :model="params" ref="loginForm" :rules="rules" label-width="100px">
          <el-form-item :label="translate('component.login.2')" prop="username">
            <el-input v-model="params.username" :placeholder="translate('component.login.3')"></el-input>
          </el-form-item>
          <el-form-item :label="translate('component.login.4')" prop="password" class="mt40">
            <el-input type="password" v-model="params.password" :placeholder="translate('component.login.5')"></el-input>
          </el-form-item>
        </el-form>
        <div class="modal-form-btn mv40">
          <button @click="handleLogin">{{ translate('component.login.6') }}</button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import CuHeader from '@/components/header';
import CuFooter from '@/components/footer';
import Api from '@/api';
import { Domain } from '@/constants';
import { isEmail } from '@/utils';
import { doLogin } from '@/utils/google';
import { doLogin as fbLogin } from '@/utils/facebook';
export default {
  name: 'Register',
  components: { CuHeader, CuFooter },
  data() {
    return {
      loading:null,
       param:{
           code:"",
           state:""
        },
      width: '800',
      domain: Domain,
      uploadUrl: Api.uploadUrl,
      title: '',
      form: {
        avatar: '',
        password: '',
        rePassword: '',
        no: '',
        mobile: '',
        email: '',
        area: [],
        address: '',
      },
      rules: {
        // avatar: { required: true, message: '请上传头像', trigger: 'change' },
        password: { required: true, message: window.$lang.t('register.20'), trigger: 'blur' },
        rePassword: [
          { required: true, message: window.$lang.t('register.21'), trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (this.form.password !== value) {
                callback(new Error(window.$lang.t('register.22')));
              } else {
                callback();
              }
            },
            trigger: 'blur',
          },
        ],
        no: { required: true, message: window.$lang.t('register.23'), trigger: 'blur' },
        mobile: [
          { required: true, message: window.$lang.t('register.24'), trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (!/\d{9}/.test(value)) {
                callback(new Error(window.$lang.t('register.29')));
              } else {
                callback();
              }
            },
            trigger: 'blur',
          },
        ],
        email: [
          { required: true, message: window.$lang.t('register.25'), trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (!isEmail(value)) {
                callback(new Error(window.$lang.t('register.26')));
              } else {
                callback();
              }
            },
            trigger: 'blur',
          },
        ],
        area: { required: true, message: window.$lang.t('register.27'), trigger: 'change' },
        address: { required: true, message: window.$lang.t('register.28'), trigger: 'blur' },
      },
      multiOptions: [],
      // 登陆弹框
      show: false,
      params: {
        capture: '',
        username: '',
        password: '',
        uuid: this.uuid,
      },
      rules2: {
        username: [
          { required: true, message: window.$lang.t('register.12'), trigger: 'blur' },
          // {
          //   validator: (rule, value, callback) => {
          //     if (!isEmail()) {
          //       callback(new Error('邮箱不合法'));
          //     } else {
          //       callback();
          //     }
          //   },
          //   trigger: 'blur',
          // },
        ],
        password: [{ required: true, message: window.$lang.t('register.20'), trigger: 'blur' }],
      },
      uuid:null,
      authParam:{
        authUuid:"",
        source:""
      },
    };
  },
  created() {
    this.authParam.authUuid = this.$route.query.uuid
    this.authParam.source = this.$route.query.source
    if(!this.$route.query.uuid||!this.$route.query.source){
      this.$router.push('*')
    }
    if (this.authParam.source === 'facebook'){
      this.form.no = this.$route.query.authInfo.name
      this.form.avatar = this.$route.query.authInfo.head
      this.form.email = this.$route.query.authInfo.email
      // eslint-disable-next-line no-empty
    }else if(this.authParam.source === 'google'){
      this.form.no = this.$route.query.authInfo.sd
      this.form.avatar = this.$route.query.authInfo.wI
      this.form.email = this.$route.query.authInfo.lt
    }
    console.log("路由过来的参数"+this.$route.query.authInfo)
    this.loadTree();
  },
  methods: {
    handleAvatarSuccess(file) {
      console.log(file)
      if (file) {
        this.form.avatar = file.path;
      }
    },
    loadTree() {
      Api.areaTree().then((res) => {
        this.multiOptions = res;
      });
    },
    handleSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form = Object.assign(this.form, this.authParam)
          this.form.areaCode = this.form.area[this.form.area.length - 1];
          Api.register(this.form).then((res) => {
            if (res) {
              window.localStorage.setItem('TOKEN', res.token);
              this.$router.go(-1);
            }
          });
        } else {
          return false;
        }
      });
    },
    handleBack() {
      this.$router.go(-1);
    },
     translate(str) {
      return window.$lang.t(str);
    },
    handleLogin() {
      console.log(this.param)
      this.$refs['loginForm'].validate((valid) => {
        if (valid) {
          this.params = {...this.params,...this.authParam}
          Api.login(this.params).then((res) => {
            if (res.token) {
              window.localStorage.setItem('TOKEN', res.token);
              this.show = false;
              this.$router.push("/")
            }
          });
        } else {
          console.log('error!');
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.register {
  min-height: 100vh;
  background: #f2f2f2;
  .register-container {
    padding: 50px 0;
    .register-form {
      padding: 20px;
      background: #fff;
      border-radius: 4px;
      .title {
        color: #333;
        font-weight: bold;
        font-size: 16px;
        padding-left: 15px;
        margin-top: 20px;
        margin-bottom: 20px;
      }
      .form {
        width: 60%;
      }
    }
  }
  .avatar-uploader {
    ::v-deep .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      &:hover {
        border-color: #fb8c24;
      }
      .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 80px;
        height: 80px;
        line-height: 80px;
        text-align: center;
      }
      .avatar {
        width: 80px;
        height: 80px;
        display: block;
      }
    }
  }
}
.modal-form {
  width: 400px;
  text-align: center;
  margin: 0 auto;
  .login-title {
    color: #595959;
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 40px;
  }
  ::v-deep .el-form-item__label {
    text-align: left;
  }
}

.modal-form-btn button {
  width: 100%;
  height: 48px;
  background: #fb7924;
  border-radius: 4px;
  color: #fff;
  line-height: 47px;
  border: none;
  outline: none;
  cursor: pointer;
  &:hover {
    background: #fdaf66;
  }
  &:active {
    background: #e27e20;
  }
}
.modal-form-register {
  font-size: 16px;
  color: #fb8c24;
}
.modal-form-forget {
  font-size: 16px;
  color: #595959;
}

.seperate {
  display: flex;
  align-items: center;
  margin: 16px 0;
  color: #999999;
  font-size: 16px;
  line-height: 24px;
  border-color: #ebedf0;
  border-style: solid;
  border-width: 0;
}
.seperate::before,
.seperate::after {
  content: '';
  display: block;
  -webkit-box-flex: 1;
  -webkit-flex: 1;
  flex: 1;
  box-sizing: border-box;
  height: 1px;
  border-color: inherit;
  border-style: inherit;
  border-width: 1px 0 0;
}

.seperate::before {
  margin-right: 16px;
}
.seperate::after {
  margin-left: 16px;
}
.login-btns {
  margin: 40px 0;
}
.btn-facebook {
  width: 170px;
  text-align: center;
  line-height: 50px;
  height: 50px;
  line-height: 50px;
  border: 1px solid #39558e;
  color: #39558e;
  margin-right: 8px;
  border-radius: 4px;
  cursor: pointer;
}
.btn-google {
  width: 170px;
  text-align: center;
  line-height: 50px;
  height: 50px;
  line-height: 50px;
  border: 1px solid #ea4335;
  color: #ea4335;
  margin-left: 8px;
  border-radius: 4px;
  cursor: pointer;
}
.btn-facebook:hover,
.btn-google:hover {
  opacity: 0.8;
  background-color: #ddd;
}
.icon-facebook {
  display: inline-block;
  width: 32px;
  height: 32px;
  margin-right: 18px;
  border-radius: 100%;
  text-align: center;
  line-height: 32px;
  background-color: #39558e;
  color: #fff;
}
.icon-google {
  display: inline-block;
  width: 32px;
  height: 32px;
  margin-right: 18px;
  border-radius: 100%;
  text-align: center;
  line-height: 32px;
  background-color: #ea4335;
  color: #fff;
}
.provicy {
  color: #999999;
  cursor: pointer;
}
.provicy-title {
  text-align: center;
  font-weight: bold;
  font-size: 18px;
  line-height: 30px;
}
.provicy-content {
  text-align: left;
  line-height: 1.5;
}
.provicy-sub {
  font-weight: bold;
  line-height: 2;
}
.go-login{
  color: #ea4335;
  cursor: pointer;
  text-decoration: underline;
  margin: 20px 0 30px 50px;
}
</style>
